package com.navi.mould1.controller;

import com.alibaba.fastjson.JSON;
import com.navi.bean.in.ConditionBean;
import com.navi.mould1.service.*;
import com.navi.mould1.service.impl.BasicDataServiceImpl;
import com.navi.mould1.tx.xpbisdata.XpbisdataO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import java.io.IOException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import static com.navi.mould1.reportUtils.reportUtil.getCondition;

@Slf4j
@Controller
public class ExportController {

	@Autowired
	private HttpServletRequest request;

	@Autowired
	private HttpServletResponse response;

	@Autowired
	private IBasicDataService bisDataService;


	/*** halm机测试-源数据导出 - Report **/
	@Autowired
	private IHalmSourceDataExportReportService halmSourceDataExportReportService;
	@ResponseBody
	@RequestMapping(value = "halm_exportExcel.do", method = RequestMethod.POST)
	public void halm_exportExcel_report(HttpServletRequest request, HttpServletResponse response) {
		String title = request.getParameter("title");
		String data = request.getParameter("data");
		ConditionBean conditions = getCondition(data);
 		halmSourceDataExportReportService.generateExcel(conditions, response);
	}


	/*** 导入基础信息 **/
	@RequestMapping("/uploadBisData.do")
	@ResponseBody
	public String uploadBisData(@RequestParam(value = "file", required = false) MultipartFile file, Model model, HttpServletResponse response, HttpServletRequest request) throws IOException, ServletException {
		String filename = file.getOriginalFilename();
		XpbisdataO xpbisdataO = new XpbisdataO();
		//1、获取excel文件
		if (file != null) {
			//是否是excel
			if (filename.matches("^.+\\.(?i)((xls)|(xlsx))$")) {
				//2、导入
				String rtn_mesg = bisDataService.importExcel(file, filename, request);
				xpbisdataO.setRtn_mesg(rtn_mesg);
			}
		}
		return JSON.toJSONString(xpbisdataO);
	}

}
